
// Source File Name:   ADImpl.java

package com.sunwayhorizo.health.hl7.datatypes.common;

import com.sunwayhorizo.health.hl7.common.Hl7Exception;
import com.sunwayhorizo.health.hl7.common.VersionInfo;
import com.sunwayhorizo.health.hl7.datatypes.AD;
import com.sunwayhorizo.health.hl7.datatypes.ADXP;
import com.sunwayhorizo.health.hl7.datatypes.ANY;
import com.sunwayhorizo.health.hl7.datatypes.BL;
import com.sunwayhorizo.health.hl7.datatypes.CS;
import com.sunwayhorizo.health.hl7.datatypes.GTS;
import com.sunwayhorizo.health.hl7.datatypes.SET_CS;
import com.sunwayhorizo.health.hl7.datatypes.ST;
import com.sunwayhorizo.health.hl7.datatypes.TYPE;

import java.util.Iterator;
import java.util.List;



//            LIST_ADXPAdapterImpl, ADXPImpl, ANYImpl, BLImpl,
//            CSImpl, GTSBase, GTSNullImpl, INTImpl, 
//            LISTAdapterCommonImpl, NullFlavorImpl, SETCommonImpl, SET_CSImpl, 
//            STCommonImpl, TYPEImpl

public class ADImpl extends LIST_ADXPAdapterImpl
    implements AD
{

    public static final String RCS_ID = "$Header: ADImpl.java 1.0 2006/10/18 06:47:31 mvarman noship $";
    public static final boolean RCS_ID_RECORDED = VersionInfo.recordClassVersion("$Header: ADImpl.java 1.0 2006/05/24 03:05:31 rassubra noship $", "com.sunwayhorizo.health.hl7.datatypes.common");
    protected SET_CS _use;
    protected GTS _useablePeriod;
    private String _partySite;
    private ST _formatted;
    private static final String USE = "use";
    private static final String USEABLE_PERIOD = "useablePeriod";
    private static final AD nullNI = new ADImpl(CSImpl.valueOf("NI"));
    private static final AD nullOTH = new ADImpl(CSImpl.valueOf("OTH"));
    private static final AD nullNINF = new ADImpl(CSImpl.valueOf("NINF"));
    private static final AD nullPINF = new ADImpl(CSImpl.valueOf("PINF"));
    private static final AD nullUNK = new ADImpl(CSImpl.valueOf("UNK"));
    private static final AD nullASKU = new ADImpl(CSImpl.valueOf("ASKU"));
    private static final AD nullNAV = new ADImpl(CSImpl.valueOf("NAV"));
    private static final AD nullMSK = new ADImpl(CSImpl.valueOf("MSK"));
    private static final AD nullNA = new ADImpl(CSImpl.valueOf("NA"));
    private static final AD nullNASK = new ADImpl(CSImpl.valueOf("NASK"));

    public static AD valueOf(List list, SET_CS set_cs, GTS gts, BL bl)
    {
        return new ADImpl(list, set_cs, gts, bl);
    }

    public static AD valueOf(ADXP aadxp[], SET_CS set_cs, GTS gts, ST st)
    {
        return new ADImpl(aadxp, set_cs, gts, st);
    }

    public static AD valueOf(ADXP aadxp[], SET_CS set_cs, GTS gts, String s, ST st)
    {
        ADImpl adimpl = new ADImpl(aadxp, set_cs, gts, st); 
        adimpl._partySite = s;
        return adimpl;
    }

    protected ADImpl(List list, SET_CS set_cs, GTS gts, BL bl)
    {
        super(list);
        _use = set_cs;
        _useablePeriod = gts;
        _partySite = null;
    }

    protected ADImpl(CS cs)
    {
        super(cs);
        _use = SET_CSImpl.nullSET_CS(NullFlavorImpl.NI);
        _useablePeriod = GTSNullImpl.nullGTS(NullFlavorImpl.NI);
        _partySite = null;
    }

    protected ADImpl(ADXP aadxp[], SET_CS set_cs, GTS gts, ST st)
    {
        super(aadxp);
        _use = set_cs;
        _useablePeriod = gts;
        _formatted = st;
    }

    public SET_CS use()
    {
        if(isNull().isTrue())
            return SET_CSImpl.nullSET_CS();
        else
            return _use;
    }

    public GTS useablePeriod()
    {
        if(isNull().isTrue())
            return GTSNullImpl.nullGTS(NullFlavorImpl.NI);
        else
            return _useablePeriod;
    }

    public ST formatted()
    {
//        throw ANYImpl.newUnsupportedException("AD", "formatted");
        return _formatted;
    }

    public TYPE dataType()
    {
        return com.sunwayhorizo.health.hl7.datatypes.common.TYPEImpl.AD;
    }

    public BL equal(ANY any)
    {
        if(!(any instanceof AD))
            return BLImpl.FALSE;
        if(isNull().or(any.isNull()).isTrue())
            return BLImpl.FALSE;
        AD ad = (AD)any;
        BL bl = length().equal(ad.length());
        for(Iterator iterator = listIterator(); bl.isTrue() && iterator.hasNext(); bl = ad.contains((ADXP)iterator.next()));
        return bl;
    }

    public String getPartySite()
    {
        return _partySite;
    }

    public int hashCode()
    {
        int i = 17;
        if(!isNull().isTrue())
            i += mList.hashCode();
        return i;
    }

//    public XMLNode[] writeXML(XMLDocument xmldocument, String s)
//        throws Hl7Exception
//    {
//        try
//        {
//            XMLElement xmlelement = (XMLElement)xmldocument.createElement(s);
//            if(use() != null && !use().isNull().isTrue())
//            {
//                XMLNode axmlnode[] = ((SET_CSImpl)use()).writeXML(xmldocument, "use");
//                xmlelement.setAttribute("use", axmlnode[0].getAttributes().getNamedItem("code").getNodeValue());
//            }
//            for(int i = 0; i < length().intValue(); i++)
//            {
//                ADXP adxp = item(INTImpl.valueOf(i));
//                if(((ADXPImpl)adxp).partType() != null)
//                    if(((ADXPImpl)adxp).partType().isNull().isFalse())
//                    {
//                        XMLNode xmlnode = ((ADXPImpl)adxp).writeXML(xmldocument, s)[0];
//                        if(xmlnode != null)
//                            xmlelement.appendChild(xmlnode);
//                    } else
//                    {
//                        String s1 = ((ADXPImpl)adxp).stringValue();
//                        if(s1 != null)
//                        {
//                            org.w3c.dom.Text text = xmldocument.createTextNode(s1);
//                            xmlelement.appendChild(text);
//                        }
//                    }
//            }
//
//            if(useablePeriod() != null && !useablePeriod().isNull().isTrue())
//                xmlelement.appendChild(((GTSBase)useablePeriod()).writeXML(xmldocument, "usablePeriod")[0]);
//            return (new XMLNode[] {
//                xmlelement
//            });
//        }
//        catch(Exception exception)
//        {
//            throw new Hl7Exception(exception);
//        }
//    }

    public static AD nullAD(CS cs)
    {
        String s = null;
        if(cs == null || cs.literal().stringValue() == null || cs.isNull().isTrue())
            s = "NI";
        else
            s = cs.literal().stringValue();
        if(s.equals("NI"))
            return nullNI;
        if(s.equals("OTH"))
            return nullOTH;
        if(s.equals("NINF"))
            return nullNINF;
        if(s.equals("PINF"))
            return nullPINF;
        if(s.equals("UNK"))
            return nullUNK;
        if(s.equals("ASKU"))
            return nullASKU;
        if(s.equals("NAV"))
            return nullNAV;
        if(s.equals("MSK"))
            return nullMSK;
        if(s.equals("NA"))
            return nullNA;
        if(s.equals("NASK"))
            return nullNASK;
        else
            return nullNI;
    }

}
